/**
 * Created with IntelliJ IDEA.
 * Description:给定一个整型数组, 实现冒泡排序(升序排序)
 * User: xwl
 * Date: 2023-04-05
 * Time: 22:16
 */
public class Test4 {
    public static void main(String[] args) {
        //定义一个整形数组
        int[] array = {2,1,3,4,8,6,5,7};
        //调用函数
        buddleSort(array);
        //输出
        for (int x: array) {
            System.out.print(x+" ");
        }

    }
    //冒泡排序
    public static void buddleSort(int[] array) {
        //使第n冒泡不用再遍历整个数组
        int count = 0;
        //实现冒泡
        for (int i = 0; i < array.length; i++) {
            //定义一个变量实现交换
            int tmp = 0;
            for (int j = 0; j < array.length - 1 - count; j++) {
                if (array[j] > array[j+1]) {
                    tmp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = tmp;
                }
            }
        count++;
        }
    }
}
